package com.test.demo;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.DependsOn;
import org.apache.tomcat.jdbc.pool.DataSource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.sql.SQLException;


@SpringBootApplication
@ComponentScan(basePackages = "com.test.demo")
@MapperScan(basePackages = "com.test.demo.mapper",sqlSessionFactoryRef = "sqlSessionFactory")
public class Main {

    public static void main(String[] args) {
        SpringApplication.run(Main.class,args);
    }

    @Bean("masterDatasource")
    @ConfigurationProperties(prefix = "spring.datasource.master")
    public DataSource masterDatasource(){
        return new DataSource();
    }

    @Bean(name = "sqlSessionFactory")
    @ConfigurationProperties(prefix = "spring.mybatis")
    @DependsOn("masterDatasource")
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Throwable {
        SqlSessionFactoryBean bean=new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
//        bean.setTypeAliasesPackage("com.test.demo.entity");
//        Resource[] resources = new PathMatchingResourcePatternResolver()
//                        .getResources("classpath:mapper/*.xml");
//        bean.setMapperLocations(resources);
        return bean.getObject();
    }

}
